package com.tech.connect.huanzhewan;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;

import com.tech.connect.R;
import com.tech.connect.activity.ModelMainActivity;
import com.tech.connect.fragment.BaseFragment;
import com.tech.connect.view.MenuTabView;

import java.util.ArrayList;
import java.util.List;

public class HuanZheWanMainFragment extends BaseFragment implements View.OnClickListener {

    private int currentIndex = -1;
    private List<MenuTabView> tabViewList;
    private HuanZheWanHomeFragment huanZheWanHomeFragment;
    private HuanZheWanHuHuanShenFenFragment huanZheWanHuHuanShenFenFragment;
    private HuanZheWanFaShaoYouFragment huanZheWanFaShaoYouFragment;
    private HuanZheWanWanPinGuiFragment huanZheWanWanPinGuiFragment;
    private ModelMainActivity.TitleListener tiTleListener;


    @Override
    protected View onCreateView(LayoutInflater inflater) {
        View inflate = inflater.inflate(R.layout.layout_tab_main, null);
        return (inflate);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }


    private void initView(View view) {
        MenuTabView tab0 = view.findViewById(R.id.tab0);
        MenuTabView tab1 = view.findViewById(R.id.tab1);
        MenuTabView tab2 = view.findViewById(R.id.tab2);
        MenuTabView tab3 = view.findViewById(R.id.tab3);
        MenuTabView tab4 = view.findViewById(R.id.tab4);
        tab4.setVisibility(View.GONE);
        tab0.setTab(MenuTabView.TabMenu.HuanZheWan_Home);
        tab1.setTab(MenuTabView.TabMenu.HuanZheWan_HuHuanShenFen);
        tab2.setTab(MenuTabView.TabMenu.HuanZheWan_FaShaoYou);
        tab3.setTab(MenuTabView.TabMenu.HuanZheWan_WanPinGui);
        tabViewList = new ArrayList<>();
        tabViewList.add(tab0);
        tabViewList.add(tab1);
        tabViewList.add(tab2);
        tabViewList.add(tab3);
        for (int i = 0; i < tabViewList.size(); i++) {
            MenuTabView menuTabView = tabViewList.get(i);
            menuTabView.setTag(i);
            menuTabView.setOnClickListener(this);
        }
        tabViewList.get(0).performClick();
    }

    @Override
    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag != null && tag instanceof Integer) {
            int index = (int) tag;
            if (currentIndex == index) {
                return;
            }
            currentIndex = index;
            showFragment(currentIndex);
            for (int i = 0; i < tabViewList.size(); i++) {
                MenuTabView menuTabView = tabViewList.get(i);
                if (currentIndex == i) {
                    menuTabView.setSelected(true);
                } else {
                    menuTabView.setSelected(false);
                }
            }
        }
    }

    private void showFragment(int index) {
        if (index == 0) {
            if (huanZheWanHomeFragment == null) {
                huanZheWanHomeFragment = new HuanZheWanHomeFragment();
                huanZheWanHomeFragment.setTitleListener(new TitleListener() {
                    @Override
                    public void onFragmentTitleClick() {
                        if (tiTleListener != null) {
                            tiTleListener.onChangeMain();
                        }
                    }
                });
                addFragment(R.id.container, huanZheWanHomeFragment);
            }
            showFragment(huanZheWanHomeFragment);
            hideFragment(huanZheWanHuHuanShenFenFragment);
            hideFragment(huanZheWanFaShaoYouFragment);
            hideFragment(huanZheWanWanPinGuiFragment);
        } else if (index == 1) {
            if (huanZheWanHuHuanShenFenFragment == null) {
                huanZheWanHuHuanShenFenFragment = new HuanZheWanHuHuanShenFenFragment();
                addFragment(R.id.container, huanZheWanHuHuanShenFenFragment);
            }
            hideFragment(huanZheWanHomeFragment);
            showFragment(huanZheWanHuHuanShenFenFragment);
            hideFragment(huanZheWanFaShaoYouFragment);
            hideFragment(huanZheWanWanPinGuiFragment);
        } else if (index == 2) {
            if (huanZheWanFaShaoYouFragment == null) {
                huanZheWanFaShaoYouFragment = new HuanZheWanFaShaoYouFragment();
                addFragment(R.id.container, huanZheWanFaShaoYouFragment);
            }
            hideFragment(huanZheWanHomeFragment);
            hideFragment(huanZheWanHuHuanShenFenFragment);
            showFragment(huanZheWanFaShaoYouFragment);
            hideFragment(huanZheWanWanPinGuiFragment);
        } else if (index == 3) {
            if (huanZheWanWanPinGuiFragment == null) {
                huanZheWanWanPinGuiFragment = new HuanZheWanWanPinGuiFragment();
                addFragment(R.id.container, huanZheWanWanPinGuiFragment);
            }
            hideFragment(huanZheWanHomeFragment);
            hideFragment(huanZheWanHuHuanShenFenFragment);
            hideFragment(huanZheWanFaShaoYouFragment);
            showFragment(huanZheWanWanPinGuiFragment);
        }
    }

    public void setTiTleListener(ModelMainActivity.TitleListener tiTleListener) {
        this.tiTleListener = tiTleListener;
    }

    public interface TitleListener {
        void onFragmentTitleClick();
    }
}
